package com.hxy.ssm.controller;

import com.hxy.ssm.bean.User;
import com.hxy.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    UserService userService;
    /**
     * 向用户登陆界面跳转
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String toLogin() {
        return "login";
    }

    /**
     * 向用户注册界面跳转
     */
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String toRegister() {
        return "register";
    }
    /**
     * 用户登陆
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(User user, Model model, HttpSession session) {
        //获取用户名和密码
        String username = user.getUsername();
        String password = user.getPassword();
        boolean ex=userService.findUserByName(username,password);
        if(ex)
        {
            //将对象用户添加到Session
            session.setAttribute("USER_SESSION",user);
            //重定向都啊主页面的跳转方法
            return "redirect:main";
        }
        model.addAttribute("msg","用户名或密码错误，请重新登录！");
        return "login";
    }

//        //此处模拟从数据库中获取用户名和密码后进行判断
//        if (username != null && username.equals("耿若兰")
//                && password != null && password.equals("18044106")) {
//            //将对象用户添加到Session
//            session.setAttribute("USER_SESSION",user);
//            //重定向都啊主页面的跳转方法
//            return "redirect:main";
//        }
//        model.addAttribute("msg","用户名或密码错误，请重新登录！");
//        return "login";
//    }
    /**
     * 向用户主页面跳转
     */
    @RequestMapping(value = "/main")
    public String toMain(){
        return "main";
    }
    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        //清除Session
        session.invalidate();
        //重定向到登录页面的跳转方法
        return "redirect:login";
    }
}

